WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
buffer.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// Wolf includes
22#include "core/common/wolf.h"
23
24namespace wolf
25{
30template <typename T>
31class Buffer
32{
33 public:
34 typedef typename std::map<TimeStamp, T>::iterator Iterator; // buffer iterator
35 typedef typename std::map<TimeStamp, T>::const_iterator ConstIterator; // buffer iterator
36
37 Buffer(){};
38 ~Buffer(void){};
39
45 T select(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
46
52 ConstIterator selectIterator(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
53 Iterator selectIterator(const TimeStamp& _time_stamp, const double& _time_tolerance);
54
55 T selectFirstBefore(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
56
57 T selectLastAfter(const TimeStamp& _time_stamp, const double& _time_tolerance) const;
58
59 T selectFirst() const;
60
61 T selectLast() const;
62
66 SizeStd size(void) const;
67
71 void emplace(const TimeStamp& _time_stamp, const T& _element);
72
77 const std::map<TimeStamp, T>& getContainer() const;
78 std::map<TimeStamp, T>& getContainer();
79
83 void removeUpTo(const TimeStamp& _time_stamp);
84
88 void removeUpToLower(const TimeStamp& _time_stamp);
89
93 void clear();
94
98 bool empty() const;
99
105 static bool checkTimeTolerance(const TimeStamp& _time_stamp1,
106 const TimeStamp& _time_stamp2,
107 const double& _time_tolerance);
108
114 static bool doubleCheckTimeTolerance(const TimeStamp& _time_stamp1,
115 const double& _time_tolerance1,
116 const TimeStamp& _time_stamp2,
117 const double& _time_tolerance2);
118
119 protected:
120 std::map<TimeStamp, T> container_; // Main buffer container
121};
122
123} // namespace wolf
124
126
127namespace wolf
128{
129template <typename T>
131 const double& _time_tolerance) const
132{
133 Buffer<T>::ConstIterator post = container_.upper_bound(_time_stamp);
134
135 bool prev_exists = (post != container_.begin());
136 bool post_exists = (post != container_.end());
137
138 bool post_ok = post_exists && checkTimeTolerance(post->first, _time_stamp, _time_tolerance);
139
140 if (prev_exists)
141 {
142 Buffer<T>::ConstIterator prev = std::prev(post);
143
144 bool prev_ok = checkTimeTolerance(prev->first, _time_stamp, _time_tolerance);
145
146 if (prev_ok && !post_ok)
147 return prev;
148
149 else if (!prev_ok && post_ok)
150 return post;
151
152 else if (prev_ok && post_ok)
153 {
154 if (std::fabs(post->first - _time_stamp) < std::fabs(prev->first - _time_stamp))
155 return post;
156 else
157 return prev;
158 }
159 }
160 else if (post_ok)
161 return post;
162
163 return container_.end();
164}
165
166template <typename T>
167inline typename Buffer<T>::Iterator Buffer<T>::selectIterator(const TimeStamp& _time_stamp,
168 const double& _time_tolerance)
169{
170 Buffer<T>::Iterator post = container_.upper_bound(_time_stamp);
171
172 bool prev_exists = (post != container_.begin());
173 bool post_exists = (post != container_.end());
174
175 bool post_ok = post_exists && checkTimeTolerance(post->first, _time_stamp, _time_tolerance);
176
177 if (prev_exists)
178 {
179 Buffer<T>::Iterator prev = std::prev(post);
180
181 bool prev_ok = checkTimeTolerance(prev->first, _time_stamp, _time_tolerance);
182
183 if (prev_ok && !post_ok)
184 return prev;
185
186 else if (!prev_ok && post_ok)
187 return post;
188
189 else if (prev_ok && post_ok)
190 {
191 if (std::fabs(post->first - _time_stamp) < std::fabs(prev->first - _time_stamp))
192 return post;
193 else
194 return prev;
195 }
196 }
197 else if (post_ok)
198 return post;
199
200 return container_.end();
201}
202
203template <typename T>
204inline T Buffer<T>::select(const TimeStamp& _time_stamp, const double& _time_tolerance) const
205{
206 if (container_.empty()) return T(); // nullprt in case of T being a pointer
207
208 auto it = selectIterator(_time_stamp, _time_tolerance);
209
210 // end is returned from selectIterator if an element of the buffer complying with the time stamp
211 // and time tolerance has not been found
212 if (it != container_.end())
213 {
214 return it->second;
215 }
216
217 return T(); // nullprt in case of T being a pointer
218}
219
220template <typename T>
221inline T Buffer<T>::selectFirstBefore(const TimeStamp& _time_stamp, const double& _time_tolerance) const
222{
223 // There is no element
224 if (container_.empty()) return T(); // nullprt in case of T being a pointer
225
226 // Checking on begin() since elements are ordered in time
227 // Return first element if is older than time stamp
228 if (container_.begin()->first < _time_stamp) return container_.begin()->second;
229
230 // Return first element if despite being newer, it is within the time tolerance
231 if (checkTimeTolerance(container_.begin()->first, _time_stamp, _time_tolerance)) return container_.begin()->second;
232
233 // otherwise return nullptr (no element before the provided ts or within the tolerance was found)
234 return T(); // nullprt in case of T being a pointer
235}
236
237template <typename T>
238inline T Buffer<T>::selectLastAfter(const TimeStamp& _time_stamp, const double& _time_tolerance) const
239{
240 // There is no element
241 if (container_.empty()) return T(); // nullprt in case of T being a pointer
242
243 // Checking on rbegin() since elements are ordered in time
244 // Return last element if is newer than time stamp
245 if (container_.rbegin()->first > _time_stamp) return container_.rbegin()->second;
246
247 // Return last element if despite being older, it is within the time tolerance
248 if (checkTimeTolerance(container_.rbegin()->first, _time_stamp, _time_tolerance))
249 return container_.rbegin()->second;
250
251 // otherwise return nullptr (no element after the provided ts or within the tolerance was found)
252 return T(); // nullprt in case of T being a pointer
253}
254
255template <typename T>
257{
258 // There is no element
259 if (container_.empty()) return T(); // nullprt in case of T being a pointer
260
261 // Returning first map element
262 return container_.begin()->second;
263}
264
265template <typename T>
266inline T Buffer<T>::selectLast() const
267{
268 // There is no element
269 if (container_.empty()) return T(); // nullprt in case of T being a pointer
270
271 // Returning last map element
272 return container_.rbegin()->second;
273}
274
275template <typename T>
276inline void Buffer<T>::emplace(const TimeStamp& _time_stamp, const T& _element)
277{
278 container_.emplace(_time_stamp, _element);
279}
280
281template <typename T>
282inline const std::map<TimeStamp, T>& Buffer<T>::getContainer() const
283{
284 return container_;
285}
286
287template <typename T>
288inline std::map<TimeStamp, T>& Buffer<T>::getContainer()
289{
290 return container_;
291}
292
293template <typename T>
294inline void Buffer<T>::clear()
295{
296 container_.clear();
297}
298
299template <typename T>
300inline bool Buffer<T>::empty() const
301{
302 return container_.empty();
303}
304
305template <typename T>
306inline SizeStd Buffer<T>::size(void) const
307{
308 return container_.size();
309}
310
311template <typename T>
312inline void Buffer<T>::removeUpTo(const TimeStamp& _time_stamp)
313{
314 Buffer::Iterator post = container_.upper_bound(_time_stamp);
315 container_.erase(container_.begin(), post); // erasing by range
316}
317
318template <typename T>
319inline void Buffer<T>::removeUpToLower(const TimeStamp& _time_stamp)
320{
321 Buffer::Iterator post = container_.lower_bound(_time_stamp);
322 container_.erase(container_.begin(), post); // erasing by range
323}
324
325template <typename T>
326inline bool Buffer<T>::doubleCheckTimeTolerance(const TimeStamp& _time_stamp1,
327 const double& _time_tolerance1,
328 const TimeStamp& _time_stamp2,
329 const double& _time_tolerance2)
330{
331 double time_diff = std::fabs(_time_stamp1 - _time_stamp2);
332 double time_tol = std::min(_time_tolerance1, _time_tolerance2);
333 bool pass = time_diff <= time_tol;
334 return pass;
335}
336
337template <typename T>
338inline bool Buffer<T>::checkTimeTolerance(const TimeStamp& _time_stamp1,
339 const TimeStamp& _time_stamp2,
340 const double& _time_tolerance)
341{
342 double time_diff = std::fabs(_time_stamp1 - _time_stamp2);
343 bool pass = time_diff <= _time_tolerance;
344 return pass;
345}
346
347// SPECIFIC BUFFERS
348// fwd declarations
349class VectorComposite;
350
353class BufferFrame : public Buffer<FrameBasePtr>
354{
355 public:
356 void emplace(FrameBasePtr _frm);
357};
358
361class BufferCapture : public Buffer<CaptureBasePtr>
362{
363 public:
364 void emplace(CaptureBasePtr _cap);
365};
366
369class BufferVectorComposite : public Buffer<VectorComposite>
370{
371};
372
373} // namespace wolf
Buffer of Captures.
Definition: buffer.h:362
void emplace(CaptureBasePtr _cap)
Definition: buffer.cpp:30
Buffer of Frames.
Definition: buffer.h:354
void emplace(FrameBasePtr _frm)
Definition: buffer.cpp:25
Buffer of VectorComposite.
Definition: buffer.h:370
Buffer for arbitrary type objects.
Definition: buffer.h:32
static bool doubleCheckTimeTolerance(const TimeStamp &_time_stamp1, const double &_time_tolerance1, const TimeStamp &_time_stamp2, const double &_time_tolerance2)
Check time tolerance.
Definition: buffer.h:326
SizeStd size(void) const
Buffer size.
Definition: buffer.h:306
std::map< TimeStamp, T >::iterator Iterator
Definition: buffer.h:34
void clear()
Clear the buffer.
Definition: buffer.h:294
const std::map< TimeStamp, T > & getContainer() const
returns the container with elements of the buffer
Definition: buffer.h:282
void removeUpTo(const TimeStamp &_time_stamp)
Remove all elements in the buffer with a time stamp older than the specified.
Definition: buffer.h:312
std::map< TimeStamp, T >::const_iterator ConstIterator
Definition: buffer.h:35
void emplace(const TimeStamp &_time_stamp, const T &_element)
Add a element to the buffer.
Definition: buffer.h:276
Iterator selectIterator(const TimeStamp &_time_stamp, const double &_time_tolerance)
Definition: buffer.h:167
T select(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Select an element from the buffer.
Definition: buffer.h:204
T selectFirst() const
Definition: buffer.h:256
Buffer()
Definition: buffer.h:37
std::map< TimeStamp, T > & getContainer()
Definition: buffer.h:288
T selectLast() const
Definition: buffer.h:266
static bool checkTimeTolerance(const TimeStamp &_time_stamp1, const TimeStamp &_time_stamp2, const double &_time_tolerance)
Check time tolerance.
Definition: buffer.h:338
T selectLastAfter(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Definition: buffer.h:238
~Buffer(void)
Definition: buffer.h:38
ConstIterator selectIterator(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Select an element iterator from the buffer.
Definition: buffer.h:130
void removeUpToLower(const TimeStamp &_time_stamp)
Remove all elements in the buffer with a time stamp older than the specified.
Definition: buffer.h:319
bool empty() const
is the buffer empty ?
Definition: buffer.h:300
T selectFirstBefore(const TimeStamp &_time_stamp, const double &_time_tolerance) const
Definition: buffer.h:221
std::map< TimeStamp, T > container_
Definition: buffer.h:120
TimeStamp implements basic functionalities for time stamps.
Definition: time_stamp.h:39
Definition: vector_composite.h:27
Definition: capture_base.h:23
std::size_t SizeStd
Other containers size type for the Wolf project.
Definition: wolf.h:66